package com.lanou3g.code0515.study;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by dllo on 18/5/15.
 */
@WebServlet(name = "UploadServlet",urlPatterns = "/upload")
//  我们也可以通过加上MultipartConfig 注解
//来获取表单中的 其他input
//建议在处理完上传之后,转发到一个单独的servlet去处理表单
//中其他的input,因为使用了该注解有可能导致FileUPLoad不好用
@MultipartConfig
public class UploadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //上传
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String code =request.getParameter("code");
        response.getWriter().println(code);

      String uploadPath = getServletContext().getRealPath("")+ File.separator+"upload";
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()){
            //创建文件夹
            uploadDir.mkdirs();
        }
        ServletFileUpload fileUpload =new ServletFileUpload();
        FileItemFactory factory = new DiskFileItemFactory();
        fileUpload.setFileItemFactory(factory);

        try {
          List<FileItem>  fileItems=fileUpload.parseRequest(request);
            if (fileItems==null){
                return;
            }
            for (FileItem fileItem : fileItems) {
                if (fileItem.isFormField()){
                    //不是一个文件,是一个表单数据
                    //input中的name属性
//                   String fieldName =fileItem.getFieldName();
//                    //name 是文件名,当不传文件时为null
//                    String name =fileItem.getName();
//                    //String是input的值
//                    //getString 参数传递编码格式
//                    //解决乱码
//                    String s =fileItem.getString("utf-8");
//                    System.out.println("fieldName"+fieldName);
//                    System.out.println("name"+name);
//                    System.out.println("S"+s);
//                    response.getWriter().println(s);
                    continue;
                }
                //源文件名
                String fileName =fileItem.getName();
                //处理后文件名
                String afterName =TestUUID.handleFileName(fileName);
                //创建文件壳子
                File file =new File(uploadDir,afterName);
                fileItem.write(file);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
